using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.UI;

namespace ASPNETVN.PORTAL.Modules.SECURITY.Controls
{
    public partial class Users : ModulePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string[] characters = new string[] {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "X", "..."};
                dlCharacters.DataSource = characters;
                dlCharacters.DataBind();
            }
        }

        protected void dlCharacters_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                StringBuilder query = new StringBuilder();
                if (e.CommandArgument.ToString().Equals("All"))
                {
                    query.Append("([Name] LIKE N'%')");
                }
                else
                {
                    query.AppendFormat("(([Name] LIKE N'{0}%') OR ([Email] LIKE N'{0}%'))", e.CommandArgument);
                }
                query.Append(" AND PortalID = " + Global.PortalID);
                odsUser.SelectParameters[0].DefaultValue = query.ToString();
            }
        }

       

        protected void btnCreateUser_Click(object sender, EventArgs e)
        {
            string url = HttpUrlBuilder.BuildAddEditUrl(this.ModuleID, 0, "AddEditUser", "");
            Response.Redirect(url);
        }

        protected void gvUser_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                string url = HttpUrlBuilder.BuildAddEditUrl(this.ModuleID, Int32.Parse(e.CommandArgument.ToString()), "AddEditUser", "");
                Response.Redirect(url);
            }
        }

        protected void odsUser_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            odsUser.SelectParameters[0].DefaultValue = "PortalID = '" + Global.PortalID + "'";
        }

        protected void btnSearch_Click(object sender, ImageClickEventArgs e)
        {
            StringBuilder query = new StringBuilder();
            if (txtSearchValue.Text.Length > 0)
            {
                query.AppendFormat("(([Name] LIKE N'%{0}%') OR ", txtSearchValue.Text);
                query.AppendFormat("([Email] LIKE N'%{0}%'))", txtSearchValue.Text);
            }
            else
            {
                query.Append("1 = 1");
            }
            query.Append(" AND PortalID = '" + Global.PortalID + "'");
            odsUser.SelectParameters[0].DefaultValue = query.ToString();
        }
    }
}